﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.Utilites;
using ADC.BL;

namespace ADC.Web.Admin
{
    public partial class ListProject : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ListProject");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.Title = Common.PAGE_TITLE + "Danh muc du an";
                BindData();
            }
        }

        private void BindData()
        {
            try
            {

                // Load project Type
                var projectType = ProjectTypeFactory.GetAll();
                DropDownListProjectType.DataValueField = "ProjectTypeID";
                DropDownListProjectType.DataTextField = "Title";
                DropDownListProjectType.DataSource = projectType;
                DropDownListProjectType.DataBind();

                BindProjectByProjectTypeID();

            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }

        }

        private void BindProjectByProjectTypeID()
        {
            // Show projects by ProjectType
            var projects = ProjectFactory.ListAllByProjectType(Convert.ToInt32(DropDownListProjectType.SelectedValue));
            GridViewProject.AutoGenerateColumns = false;
            GridViewProject.DataSource = projects;
            GridViewProject.DataBind();
        }
        protected void GridViewProject_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                var lblProjectID = (Label)GridViewProject.Rows[e.NewEditIndex].FindControl("lblProjectID");
                if (lblProjectID != null && !string.IsNullOrEmpty(lblProjectID.Text))
                {
                    Response.Redirect("ManageProject.aspx?Mode=1&ID=" + lblProjectID.Text, false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void GridViewProject_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                var lblProjectID = (Label)GridViewProject.Rows[e.RowIndex].FindControl("lblProjectID");
                if (lblProjectID != null && !string.IsNullOrEmpty(lblProjectID.Text))
                {
                    int result = ProjectFactory.Delete(Convert.ToInt32(lblProjectID.Text));
                    BindData();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void DropDownListProjectType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindProjectByProjectTypeID();
        }
    }
}